package priv.bestbeat.reportcenter.core.monitor.mybatis.typehandler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 张渠钦
 * @date 2022/4/29 15:33
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(Object.class)
public class JsonTypeHandler extends BaseTypeHandler {

    private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    static {
        OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
        try {
            String typename = parameter.getClass().getTypeName();

            String str_parameter = OBJECT_MAPPER.writeValueAsString(parameter);
            ObjectNode objectNode = (ObjectNode)OBJECT_MAPPER.readTree(str_parameter);
            objectNode.put("instance",typename);
            ps.setString(i,objectNode.toString());
        } catch (IOException e) {
            throw new SQLException(e);
        }
    }

    @Override
    public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String str = rs.getString(columnName);
        try {
            JsonNode jsonNode = OBJECT_MAPPER.readTree(str);
            String typename = jsonNode.findValue("instance").asText();
            Class<?> clazz = getClass().getClassLoader().loadClass(typename);
            return OBJECT_MAPPER.readValue(str,clazz);
        } catch (JsonProcessingException | ClassNotFoundException e) {
            throw new SQLException(e);
        }
    }

    @Override
    public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String str = rs.getString(columnIndex);
        try {
            JsonNode jsonNode = OBJECT_MAPPER.readTree(str);
            String typename = jsonNode.findValue("instance").asText();
            Class<?> clazz = getClass().getClassLoader().loadClass(typename);
            return OBJECT_MAPPER.readValue(str,clazz);
        } catch (JsonProcessingException | ClassNotFoundException e) {
            throw new SQLException(e);
        }
    }

    @Override
    public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String str = cs.getString(columnIndex);
        try {
            JsonNode jsonNode = OBJECT_MAPPER.readTree(str);
            String typename = jsonNode.findValue("instance").asText();
            Class<?> clazz = getClass().getClassLoader().loadClass(typename);
            return OBJECT_MAPPER.readValue(str,clazz);
        } catch (JsonProcessingException | ClassNotFoundException e) {
            throw new SQLException(e);
        }
    }
}
